package me.hl.collection;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Planck on 2016/1/30.
 */
public class MapDemo {

    @Test
    public void listToMap() {
        List<Pojo> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Pojo(String.valueOf(i), String.valueOf(new Random().nextInt(100))));
        }

        //list转化成map
        Map<String, String> map = list.stream().collect(Collectors.toMap(pojo -> pojo.getName(), pojo -> pojo.getValue()));

        //遍历的第一种方法
        map.forEach((key, value) -> System.out.println(key + "," + value));
    }


    @Test
    public void bianli() {
        Map<String, String> map = new HashMap<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            map.put(String.valueOf(random.nextInt(100)), String.valueOf(random.nextInt(100)));
        }
        //老版本遍历的方法
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }


    public class Pojo {
        private String name;
        private String value;

        public Pojo(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}


